if(DEFINED ColorMessageGuard)
    return()
endif()

set(ColorMessageGuard yes)

# check color if colors are possible
set(ColoursArePossible no)
if("Linux" STREQUAL "${CMAKE_HOST_SYSTEM_NAME}")
    execute_process(COMMAND tty OUTPUT_VARIABLE tty OUTPUT_STRIP_TRAILING_WHITESPACE)
    if(NOT "not a tty" STREQUAL "${tty}")
        execute_process(COMMAND tput colors OUTPUT_VARIABLE tput)
        if(${tput} GREATER 0)
            set(ColoursArePossible yes)
        endif()
    endif()
endif()

function (colormsg)
  string (ASCII 27 _escape)
  set(WHITE "29")
  set(GRAY "30")
  set(RED "31")
  set(GREEN "32")
  set(YELLOW "33")
  set(BLUE "34")
  set(MAG "35")
  set(CYAN "36")

  foreach (color WHITE GRAY RED GREEN YELLOW BLUE MAG CYAN)
    set(HI${color} "1\;${${color}}")
    set(LO${color} "2\;${${color}}")
    set(_${color}_ "4\;${${color}}")
    set(_HI${color}_ "1\;4\;${${color}}")
    set(_LO${color}_ "2\;4\;${${color}}")
  endforeach()

  set(str "")
  set(coloron FALSE)
  foreach(arg ${ARGV})
    if (NOT ${${arg}} STREQUAL "")
      if (ColoursArePossible)
        set(str "${str}${_escape}[${${arg}}m")
        set(coloron TRUE)
      endif()
    else()
      set(str "${str}${arg}")
      if (coloron)
        set(str "${str}${_escape}[0m")
        set(coloron FALSE)
      endif()
      set(str "${str} ")
    endif()
  endforeach()
  message(STATUS ${str})
endfunction()
